// 
//  FDGameMenuView.m
//  FiveDice
//  
//  Created by Chris Born.
//  Copyright 2007 Borngraphics. All rights reserved.
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//  
//  By contributing code you grant Chris Born and Borngraphics an
//  unlimited, non-exclusive license to your contribution.
//  
//  For support, questions, commercial use, etc...
//  E-Mail: chris <at> borngraphics <dot> com
// 

#import "FDGameMenuView.h"

@implementation FDGameMenuView
-(id)initWithFrame:(struct CGRect)frame {
	if ((self == [super initWithFrame: frame]) != nil) 
	{
		NSLog(@"GameMenuView %s", _cmd);
		_contentRect = [UIHardware fullScreenApplicationContentRect];
		_offScreenRect = frame;
		_onScreenRect = frame;
		_onScreenRect.origin.x = 0.0f;
		
		defaults = [[FDDefaultsController alloc] init];
		
		UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(68,170,187,135)];
		[logo setImage:[UIImage applicationImageNamed:@"templogo.png"]];	
		[self addSubview:logo];
		[logo release];
		
		[self setupButtons];			
	}
	return self;
}

#pragma mark Show and Hide Methods

- (void)show
{
	NSLog(@"%s GameMenu", _cmd);
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];

	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_offScreenRect];
	[translate setStartTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[translate setEndTransform:trans];
	[animator addAnimation:translate withDuration:.5 start:YES];
}

- (void)hide
{
	if ([self keyboardVisible]) [textInputView hide];
	
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];
	
	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_onScreenRect];
	[translate setStartTransform:trans];
	[translate setEndTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[animator addAnimation:translate withDuration:.5 start:YES];
}

#pragma mark Buttons and Graphics

- (void)setupButtons {
	float whiteColor[4] = {1,1,1,1};
	float clearColor[4] = {0,0,0,0};
	
	//UIPushButton	*player1Button = [[UIPushButton alloc] initWithFrame:CGRectMake(30,10,260,60)];
	UIPushButton	*player1Button = [[UIPushButton alloc] initWithFrame:CGRectMake(30,8,260,60)];
	[player1Button setBackground:[UIImage applicationImageNamed:@"player1.png"] forState:0];
	[player1Button setBackground:[UIImage applicationImageNamed:@"player1_dn.png"] forState:1];
	[player1Button addTarget:self action:@selector(showTextInputView:) forEvents:(255)];
	[self addSubview:player1Button];
	[player1Button release];
	
	
	UIPushButton	*player2Button = [[UIPushButton alloc] initWithFrame:CGRectMake(30,75,260,60)];
	[player2Button setBackground:[UIImage applicationImageNamed:@"player2.png"] forState:0];
	[player2Button setBackground:[UIImage applicationImageNamed:@"player2_dn.png"] forState:1];
	[player2Button setAlpha:0.4f];
	[self addSubview:player2Button];
	[player2Button release];
	
	playerOneLabel = [[UITextLabel alloc] initWithFrame:CGRectMake(98,30,180,31)];
	[playerOneLabel setText:[defaults playerOneName]];
	[playerOneLabel setShadowOffset:CGSizeMake(0,1)];
	
	struct __GSFont *complFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:20];
	[playerOneLabel setFont:complFont];
	
	[playerOneLabel setColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), whiteColor)];
	[playerOneLabel setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), clearColor)];
	[self addSubview:playerOneLabel];
	[playerOneLabel release];
	
	struct __GSFont *buttonFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:16];

	UIPushButton	*newgameButton = [[UIPushButton alloc] initWithFrame:CGRectMake(31,335,106,43)];
	[newgameButton setBackground:[UIImage applicationImageNamed:@"woodbutton_up.png"] forState:0];
	[newgameButton setBackground:[UIImage applicationImageNamed:@"woodbutton_dn.png"] forState:1];
	[newgameButton setAutosizesToFit:NO];
	[newgameButton setDrawContentsCentered:YES];
	[newgameButton setTitleFont:buttonFont];
	[newgameButton setTitle:@"New Game"];
	[newgameButton addTarget:self action:@selector(buttonSelected:) forEvents: (255)];
	[self addSubview:newgameButton];
	[newgameButton release];
	
	UIPushButton	*aboutButton = [[UIPushButton alloc] initWithFrame:CGRectMake(183,335,106,43)];
	[aboutButton setBackground:[UIImage applicationImageNamed:@"woodbutton_up.png"] forState:0];
	[aboutButton setBackground:[UIImage applicationImageNamed:@"woodbutton_dn.png"] forState:1];
	[aboutButton setAutosizesToFit:NO];
	[aboutButton setDrawContentsCentered:YES];
	[aboutButton setTitleFont:buttonFont];
	[aboutButton setTitle:@"About"];
	[aboutButton addTarget:self action:@selector(buttonSelected:) forEvents: (255)];
	[self addSubview:aboutButton];
	[aboutButton release];
	
	[self setNeedsDisplay];
}



#pragma mark Other Views
- (void)showHighScoresBanner {
	NSLog(@"%s", _cmd);
	
	if ([self keyboardVisible]) [textInputView hide];	// Hide the keyboard so the banner is not laying over it
	if (!highScoresView){
		NSLog(@"%s About to alloc a new FDHighScoresView", _cmd);
		highScoresView = [[FDHighScoresView alloc] initWithFrame:CGRectMake(-_contentRect.size.width,0,_onScreenRect.size.width,_onScreenRect.size.height)];
		[highScoresView setDelegate:self];
		[self addSubview: highScoresView];
		[highScoresView release];
	} else {
		[highScoresView reloadData];
	}
	
	// Give the data a chance to reload before sliding the view in place.
	NSTimer		*waitForIt = [NSTimer scheduledTimerWithTimeInterval:0.5
										  	  target:self
											selector:@selector(highScoreBannerDelayShow:)
											userInfo:nil
										 	 repeats:NO];
										
	[delegate changeStateTo:HIGHSCORES];
}

- (void)highScoreBannerDelayShow:(NSTimer *)aTimer
{
	[highScoresView show];
}

- (void)hideHighScoresBanner 
{
	[highScoresView hide];
}

- (void)sendClearHighScores
{
	[highScoresView reloadData];
}

- (void)showTextInputView:(UIPushButton *)button {
	NSLog(@"%s", _cmd);
	if (![button isPressed]) {
		if (!textInputView){
			NSLog(@"%s About to alloc a new TextInputView", _cmd);
			textInputView = [[FDTextInputView alloc] initWithFrame:CGRectMake(0.0f,_onScreenRect.size.height,_onScreenRect.size.width,261) forPlayer:1];
			[textInputView setDelegate:self];
			[self addSubview: textInputView];
			[textInputView release];
		}
		[textInputView show];
	}	
}

#pragma mark Delegates
- (void)buttonSelected:(UIPushButton *)button // Respond to pressed buttons
{
	NSLog(@"%s", _cmd);
	if (![button isPressed]) {
		if ([[button title] isEqualToString:@"About"]) {
			//[self showTextInputView];
			[delegate showAboutAlert];
		}
		if ([[button title] isEqualToString:@"New Game"]) {
			[delegate startNewGame];
		}
		
    }
}

- (void)hidingHighScores // Delegate method from FDHighScoresView
{
	[delegate changeStateTo:MENU];
}

- (void)updatedPlayerNames
{
	[playerOneLabel setText:[defaults playerOneName]];
}

#pragma mark Accessors

- (BOOL)keyboardVisible
{
	return keyboardVisible;
}

- (void)setKeyboardVisible:(BOOL)newKeyboardVisible
{
	keyboardVisible = newKeyboardVisible;
}

- (NSString *)playerOneName
{
	return playerOneName;
}

- (void)setPlayerOneName:(NSString *)newPlayerOneName
{
	playerOneName = newPlayerOneName;
}

- (NSString *)playerTwoName
{
	return playerTwoName;
}

- (void)setPlayerTwoName:(NSString *)newPlayerTwoName
{
	playerTwoName = newPlayerTwoName;
}

#pragma mark Delegate
- (id)delegate
{
	return delegate;
}

- (void)setDelegate:(id)newDelegate
{
	delegate = newDelegate;
}

#pragma mark Dealloc
- (void)dealloc
{
	[animator release];
	[translate release];
	if (highScoresView) [highScoresView release];
	[super dealloc];
}
@end
